// 引入接口文件
import { getMember,queryAllAddress,delPremise } from './premisesServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"
// 初始默认状态
const defultState = {
  areaList: [],
  biz: {
		queryForm: {}
	},
}

// 声明module
const tmpModule = {
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
          dispatch({ type: 'getAreaList', payload: {} });
        } 
      }) 
    },
  },
  // 方法列表
  effects: {
    *getAreaList({ payload }, { put, call, select }) {
      try {
        const data = yield call(queryAllAddress, {})
        //todo data.success //接口格式需要调整
        if (data.success) {
          let areaList = []
          let arr = [data.data[0]]
          let attr = [data.data[data.data.length-1]]
          data.data[0] = {
            label:"北京",
            value:"110100",
            children:arr
          }
          data.data[data.data.length-1]={
            label:"澳门",
            value:"820000",
            children:attr
          }
          //去掉第三级，
          areaList = makeAddress(data.data,2)
          yield put({ type: 'updateStore', payload: { areaList } });
        }
      } catch (e) {

      }
    },
    *delPremise({payload},{ put, call, select }){
      console.log(payload,'--------------')
      try{
        console.log(delPremise)
        const { data, success } = yield call(delPremise,payload);
        let defultState = yield select(state => state[tmpModule.namespace])
        if(success) {
          message.success('删除成功',()=>{
            console.log(defultState.biz)
            defultState.biz.refreshListDatas(true)
          })
        }else{
  
        }
      }catch(e){
  
      }
    },

  }, 
  
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
